﻿using MaterialDesignThemes.Wpf;

using PLCS.Domain;
using PLCS.Domain.Configs;
using PLCS.Domain.Equipments;
using PLCS.Domain.ErrorLibraries;
using PLCS.Domain.Logs;
using PLCS.Domain.Menus;
using PLCS.Domain.Orders;
using PLCS.Domain.PLCConfigs;
using PLCS.Domain.Processes;
using PLCS.Domain.Roles;
using PLCS.Domain.Solutions;
using PLCS.Domain.Stations;
using PLCS.Domain.Tasks;
using PLCS.Domain.Trays;
using PLCS.Domain.Users;

using System.Diagnostics.CodeAnalysis;

using Volo.Abp.EntityFrameworkCore.Modeling;

namespace PLCS.EntityFrameworkCore.EntityFrameworkCore
{
    public static class ManagementDbContextModelBuilderExtensions
    {
        public static void ConfigurePLCSManagement(
      [NotNull] this ModelBuilder builder)
        {
            Check.NotNull(builder, nameof(builder));

            #region 实体管理

            builder.Entity<Equipment>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Equipments", PLCSConsts.DbSchema);
                b.HasKey(x => new { x.Id, x.EquNum });
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            builder.Entity<Station>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Stations", PLCSConsts.DbSchema);
                b.HasKey(x => new { x.Id });
                b.HasMany(x => x.Trays).WithOne(x => x.Station).HasForeignKey(x => x.StationId);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<Tray>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Trays", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            #endregion 实体管理

            #region PLC配置

            builder.Entity<PLCConfig>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "PLCConfigs", PLCSConsts.DbSchema);
                b.HasMany(x => x.PLCReadDatas).WithOne(x => x.PLCConfig).HasForeignKey(x => x.PLCConfigId);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<PLCReadData>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "PLCReadDatas", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            #endregion PLC配置

            #region 工单任务指令

            builder.Entity<Order>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Orders", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<TaskModel>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Tasks", PLCSConsts.DbSchema);
                b.HasMany(x => x.Instructions).WithOne(x => x.Task).HasForeignKey(x => x.TaskId);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<Instruction>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Instructions", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            #endregion 工单任务指令

            #region 历史 工单任务指令

            builder.Entity<HisInstruction>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "HisInstructions", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<HisOrder>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "HisOrders", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<HisTask>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "HisTasks", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            #endregion 历史 工单任务指令

            #region 日志

            builder.Entity<ErrorLog>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "ErrorLogs", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            builder.Entity<InterfaceLog>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "InterfaceLogs", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<OperationLog>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "OperationLogs", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            #endregion 日志

            #region 工艺配置

            builder.Entity<OrderRunningCondition>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "OrderRunningConditions", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<Process>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Processes", PLCSConsts.DbSchema);
                b.HasKey(x => new { x.Id, x.ProcessNo });
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<Solution>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Solutions", PLCSConsts.DbSchema);
                b.HasKey(x => new { x.Id, x.SolutionNo });
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<SolutionStep>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "SolutionSteps", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            builder.Entity<Trigger>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Triggers", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            #endregion 工艺配置

            #region 基础数据

            builder.Entity<Config>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Configs", PLCSConsts.DbSchema);
                b.HasKey(x => new { x.Id, x.Name });
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<BackData>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "BackDatas", PLCSConsts.DbSchema);
                b.HasKey(x => new { x.Id, x.Key });
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<Menu>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Menus", PLCSConsts.DbSchema);
                b.HasOne(x => x.Parent).WithMany(x => x.Children);

                b.ConfigureByConvention(); //auto configure for the base class props
            });

            builder.Entity<PathResult>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "PathResults", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<InstructionTimeLog>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "InstructionTimeLogs", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<Role>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Roles", PLCSConsts.DbSchema);

                b.ConfigureByConvention(); //auto configure for the base class props
            });

            builder.Entity<User>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "Users", PLCSConsts.DbSchema);
                b.Property("PassWord");

                b.ConfigureByConvention(); //auto configure for the base class props
            });
            builder.Entity<ErrorLibrary>(b =>
            {
                b.ToTable(PLCSConsts.DbTablePrefix + "ErrorLibraries", PLCSConsts.DbSchema);
                b.ConfigureByConvention(); //auto configure for the base class props
            });

            #endregion 基础数据
        }
    }
}